import { deleted, get, post, put } from "../utils/http";
import type { IResponse } from "./types";
enum URL {
  CODE = "/user/code",
  LOGIN = "/user/login",
  REGISTRY = "/user/registry",
  EDIT = "/user/edit",
  INFO = "/user/info",
}
interface ILoginData {
  email: string;
  password: string;
  code?: string;
}
export function getCode({ email }: Pick<ILoginData, "email">) {
  return post<IResponse>({
    url: URL.CODE,
    data: {
      email,
    },
  });
}
export function login(user: ILoginData) {
  return post<IResponse>({
    url: URL.LOGIN,
    data: user,
  });
}
type IRegistry = ILoginData;
export function registry(user: IRegistry) {
  return post<IResponse>({
    url: URL.REGISTRY,
    data: user,
  });
}
export function getInfo() {
  return post<IResponse>({
    url: URL.INFO,
  });
}
interface User extends ILoginData {
  username: string;
  gender: "男" | "女" | "保密";
}
export function editUser(user: User) {
  return put<IResponse>({
    url: URL.EDIT,
    data: user,
  });
}
export function getUsers() {
  return get<IResponse>({
    url: "/user",
  });
}
export function logoff() {
  return deleted<IResponse>({
    url: "/user/logoff",
  });
}
export function putAvatar(file: File) {
  return put<IResponse>({
    url: "/user/avatar",
    data: {
      file,
    },
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
}
